#!/bin/bash

# 时间同步
sudo timedatectl set-timezone Asia/Shanghai
sudo timedatectl status
sudo dnf install chrony
sudo systemctl start chronyd
sudo systemctl enable chronyd
## 执行以下命令，查看本机时间同步状态，用于验证服务是否已启动。
chronyc tracking
## 执行以下命令，查看时间同步服务器列表。
chronyc -n sources -v

# 关闭分区
sudo swapoff -a
sudo sed -i '/ swap / s/^/#/' /etc/fstab
## 查看是否关闭(没swap)
free -m

# 关闭selinux(不关闭selinux haproxy会不成功)
sudo setenforce 0
sudo sed -i 's/SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
sudo systemctl disable firewalld.service

# 设置环境变量
cat <<EOF | sudo tee -a ~/.bashrc
export KUBECONFIG=/etc/kubernetes/kubeconfig/admin.kubeconfig
EOF
source ~/.bashrc

cat <<EOF | sudo tee /etc/hosts
192.168.56.11 master-1
192.168.56.12 master-2
192.168.56.13 master-3
EOF

# 转发 IPv4 并让 iptables 看到桥接流量 (安装和配置先决条件)
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
cls_bpf
sch_ingress
EOF
sudo modprobe overlay
sudo modprobe br_netfilter
sudo modprobe cls_bpf
sudo modprobe sch_ingress
# 通过运行以下指令确认 br_netfilter 和 overlay 模块被加载
lsmod | egrep "cls_bpf|sch_ingress|br_netfilter|overlay"

# 设置所需的 sysctl 参数，参数在重新启动后保持不变
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables  = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward                 = 1
EOF
# 应用 sysctl 参数而不重新启动
sudo sysctl --system
# 通过运行以下指令确认 net.bridge.bridge-nf-call-iptables、net.bridge.bridge-nf-call-ip6tables 和 net.ipv4.ip_forward 系统变量在你的 sysctl 配置中被设置为 1
sysctl net.bridge.bridge-nf-call-iptables net.bridge.bridge-nf-call-ip6tables net.ipv4.ip_forward


# egrep "^CONFIG_BPF=|^CONFIG_BPF_SYSCALL=|^CONFIG_NET_CLS_BPF=|^CONFIG_BPF_JIT=|^CONFIG_NET_CLS_ACT=|^CONFIG_NET_SCH_INGRESS=|^CONFIG_CRYPTO_SHA1=|^CONFIG_CRYPTO_USER_API_HASH=|^CONFIG_CGROUPS=|^CONFIG_CGROUP_BPF=" /boot/config-$(uname -r)

